import time

a = "a"
b = "b"
c = ["c"]

def foo1():
    a = "a_change"
    global b
    b = "b_change"
    c.append("c_change")
    print("此处a为函数foo1的局部变量：a =", a)
    print("此处b进行global申明，为全局变量：b =", b)
    print("此处c未进行赋值，c仍为全局变量：c =", c)
    return None

foo1()
print("此处a为全局变量，foo1的调用对全局变量a不产生影响：a =", a)
print("此处b为全局变量，b在调用foo1后发生改变：b =", b)
print("此处c为全局变量，b在调用foo1后发生改变：c =", c)

def make_sum():
    sum = 0
    def get_sum(x):
        nonlocal sum
        sum += x
        return sum
    return get_sum

sum_x = make_sum()
print(sum_x(1))
print(sum_x(2))
print(sum_x(3))
print(sum_x(4))


# 时间装饰器
def clock_it_deco(func):
    def wrapper(a, b):
        start_time = time.time()
        result = func(a, b)
        end_time = time.time()
        print(f"{func.__name__} execution time is : {format(end_time - start_time, '.2f')} s")
        return result

    return wrapper

@clock_it_deco
def foo2(a, b):
    count = 0
    while True:
        if count > a ** b:
            break
        else:
            count += 1

foo2(10, 5)



# 斐波那契装饰器
def fib_cache_deco(func):
    def wrapper(n):
        d_fib = {}
        if n in d_fib.keys():
            print(d_fib)
            return d_fib(n)
        else:
            result = func(n)
            d = {n: result}
            d_fib.update(d)
            print(d_fib)
            return result

    return wrapper


@fib_cache_deco
def fib(n):
    if n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)